package com.wangtianxiang.sxp.back;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@EnableScheduling
@Component
public class JobTest {


    @Autowired
    private RedissonClient redissonClient;

    @Scheduled(cron="0 0/1 * * * ?")
    public void run(){
        
        RLock lock = redissonClient.getLock("key");
        try {
            boolean isLock = lock.tryLock( 30000, TimeUnit.MILLISECONDS);
            System.out.println(String.format("get Lock: %s",isLock));
            if(!isLock){
                return;
            }

            try {


                System.out.println("开始执行业务逻辑 模拟耗时20秒");
                Thread.sleep(20000);
                System.out.println("结束执行业务逻辑");
            }finally {
                lock.unlock();
                System.out.println("unLock");
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
